// pages/detail/detail.js
const app = getApp()
const {
  getRandomCode
} = require("../../utils/common")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrls: [
      'https://www.maohz.com/mhzapi/api/Common/ImageFile/goodsimgderek20181027190411593924.jpg',
      'https://www.maohz.com/mhzapi/api/Common/ImageFile/goodsimgderek20181027185654542130.jpg',
      'https://www.maohz.com/mhzapi/api/Common/ImageFile/goodsimgderek20181027232329598317.jpg'
    ],
    indicatorDots: true,
    autoplay: true,
    interval: 5000,
    duration: 1000,
    goodsNo: "",
    goodsInfo: {
      GoodsNo: "20190613001",
      Title: "猫玩具鱼猫薄荷鱼逗猫棒磨牙玩具宠物毛绒仿真",
      SaleAmount: 100
    },
    current: 0,
    pageNo: 1,
    pageSize: 10,
    isNoResult: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let self = this
    self.data.goodsNo = options.goodsNo
    self.getGoodsInfo(self.data.goodsNo)
    // console.log(options)
  },
  //获取商品信息
  getGoodsInfo: function() {
    let self = this
    wx.request({
      url: `${app.globalData.host}/api/miniapps/getWXGoodsInfo`,
      data: {
        goodsNo: self.data.goodsNo
      },
      header: {
        'content-type': 'application/json'
      },
      success: (result) => { //接口调用成功的回调函数
        console.log("获取商品信息获取商品信息获取商品信息获取商品信息")
        console.log(result)
        let data = result.data
        if (data.success && data.data && data.data.length) {
          let goodsInfo = data.data[0]
          self.setData({
            goodsInfo: goodsInfo
          })
        }
      },
      fail: () => {},
      complete: () => {}
    });
  },
  //跳转到首页
  JumpToHome: function() {
    console.log("1111")
    wx.switchTab({
      url: '/pages/home/index'
    })
  },
  //大图点击预览
  showimgtop: function(e) { //展示图
    let self = this
    self.showImg(e, self.data.imgUrls[self.data.current], self.data.imgUrls)
  },
  showImg: function(e, thisurl, thisimg) {
    wx.previewImage({
      current: thisurl, //当前显示的图片路径
      urls: thisimg
    })
  },
  swiperChange: function(e) {
    let self = this
    self.setData({
      current: e.detail.current
    })
  },
  //联系客服
  onCall: function(e) {
    let self = this
    wx.makePhoneCall({
      phoneNumber: "10086"
    });
  },
  handleContact: function(e) {
    console.log(e.path)
    console.log(e.query)
  },
  //立即购买
  submit: function() {
    let self = this
    wx.request({
      url: `${app.globalData.host}/api/miniapps/submitOrder`,
      data: {
        UserId: wx.getStorageSync("userId"),
        OrderNo: getRandomCode(8),
        BuyUser: app.globalData.userInfo.nickName,
        BuyPhone: "15850217240",
        BuyAddr: "苏州市工业园区仁爱路1号",
        GoodsNo: self.data.goodsNo,
        GoodsName: self.data.goodsInfo.Title,
        Num: 1,
        AmountTotal: self.data.goodsInfo.SaleAmount,
        RealAmountTotal: self.data.goodsInfo.SaleAmount,
        ShopId: "1",
        ShopName: "忠犬八公",
        ShopPhone: "15850217240",
        DataStatus: 1,
        OrderStatus: 2,
        FlowNo: "0000001"
      },
      header: {
        'content-type': 'application/json'
      },
      method: 'POST',
      success: (result) => { //接口调用成功的回调函数
        if (result.data && result.data.success && result.data.data == "1") {
          wx.showToast({
            title: '成功',
            icon: 'success',
            duration: 30000,
            success: function(e) {
              wx.reLaunch({ //reLaunch
                url: '/pages/order/order',
              })
            }
          })

        }
      },
      fail: () => {},
      complete: () => {}
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})